<template>
  <div class="updateInfo">

    <!--编辑弹出框-->
    <el-dialog title="修改个人信息" :visible.sync="dialogVisibleforedit" width="30%">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px">
        <el-form-item label="学号" prop="sno">
          <el-input v-model="form.sno" disabled style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="sname">
          <el-input v-model="form.sname" clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="ssex">
          <el-radio v-model="form.ssex" label="男"></el-radio>
          <el-radio v-model="form.ssex" label="女"></el-radio>
        </el-form-item>
        <el-form-item label="年龄" prop="sage">
          <el-input v-model="form.sage" clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="系别" prop="sdept">
          <el-select v-model="form.sdept" placeholder="请选择系别">
            <el-option
                v-for="item in depts"
                :label="item.dname"
                :value="item.dname">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleforedit = false">取 消</el-button>
        <el-button type="primary" @click="editSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import request3 from "@/utils/request3";

export default {
  name: "UpdateInfo",
  data() {
    const checkAge = (rule, value, callback) => {
      if (!value) {
        callback(new Error('年龄不能为空'))
      }
      if (!/^[0-9]*$/.test(value)) {
        callback(new Error('请输入数字值'));
      }
      if (parseInt(value) < 21 || parseInt(value) > 24) {
        callback(new Error('年龄只能为21-24范围内'));
      }
      callback();
    };
    return {
      dialogVisibleforedit: false,
      sno: JSON.parse(Cookies.get('student')).username,
      form: {},
      depts: [],
      rules: {
        sname: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
        ],
        sage: [
          {validator: checkAge, trigger: 'blur'},
        ],
      }
    }
  },
  created() {
    this.dialogVisibleforedit = true
    request3.get('/student/' + this.sno).then(res => {
      this.form = res.data
    })
    request3.get('/student/getAllDept').then(res => {
      this.depts = res.data
    })
  },
  methods: {
    editSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request3.put('/student/update/' + this.sno, this.form).then(res => {
            if (res.code == '200') {
              this.$notify.success("编辑成功")
              this.dialogVisibleforedit = false
              this.load()
            } else {
              this.$notify.error('编辑失败')
            }
          })
        } else {
          this.$notify.warning('请填写全部信息')
        }
      })
    },
  }
}
</script>
